import { APP_KEY } from "@/stores/app"
import { request } from "./request"
import { encode } from "js-base64"

export const api = {
  async getToken() {
    return request.post('gettoken', {
      appKey: APP_KEY
    }, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }).then(res => res.data?.data?.token)
  },
  async register(data = { userName: '', passwd: '', nickName: '', mobile: '', invitation_code: '' }) {
    return request.post('reg', data).then(res => res.data)
  },
  async login(data = { userName: '', passwd: '' }) {
    return request.post('login', data).then(res => res.data)
  },
  async logout(data = { userName: '' }) {
    return request.post('logout', data).then(res => res.data)
  },
  async sign(data = { userName: '' }) {
    return request.post('sigin', data).then(res => res.data)
  },
  async getUserSig(data = { userName: '' }) {
    return request.post(`gensign/${data.userName}`).then(res => res.data)
  },
  async upload(file) {
    const formdata = new FormData()
    formdata.set('file', file)
    return request.post('upload', formdata, {
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      timeout: 30000
    }).then(res => res.data)
  },
  async coinAccountInfo() {
    return request.post('getwithinfo').then(res => res.data)
  },
  async articelDetail(id) {
    return request.post(`getdesc/${id}`).then(res => res.data)
  },
  account: {
    async changePassword(data = { oldPwd: '', newPwd: '' }) {
      return request.post('changepwd', data).then(res => res.data)
    },
    async resetPassword(data = {userName: '', mobile: '', newPwd: '' }) {
      return request.post('resetpwd', data).then(res => res.data)
    },
    async changeAvatar(data = { avatarUrl: '' }) {
      return request.post('changeavatar', data).then(res => res.data)
    },
    async inviteList(data = { invitation_code: '' }) {
      return request.post('invitelist', data).then(res => res.data)
    }
  },
  news: {
    async list(page = 0) {
      return request.post(`getmsg/${page}/20`).then(res => res.data)
    }
  },
  notice: {
    async list(page = 0) {
      return request.post(`getnotice/${page}/20`).then(res => res.data)
    }
  },
  space: {
    async list(page = 0) {
      return request.post(`getlife/${page}/20`).then(res => res.data)
    },
    async publish(data = { file_url: '', article_content: '', title: '' }) {
      return request.post('lifecircle', data).then(res => res.data) 
    }
  },
  wallet: {
    async withapply(data = { price: 0, addr: '' }) {
      return request.post('withapply', data).then(res => res.data)
    },
    async balance() {
      return request.post('searchbalance').then(res => res.data)
    },
    async withdrawList(page = 0) {
      return request.post(`withlist/${page}/20`).then(res => res.data)
    },
    async bill() {
      return request.post('conslist').then(res => res.data)
    }
  },
  friends: {
    async list() {
      return request.post('listfri').then(res => res.data)
    },
    async search(data = { userName: '' }) {
      return request.post('finduser', data).then(res => res.data)
    },
    async add(data = { friendName: '' }) {
      return request.post('addfri', data).then(res => res.data)
    },
    async remove(data = { friendName: '' }) {
      return request.post('delfri', data).then(res => res.data)
    },
    async newList() {
      return request.post('reqFriendList').then(res => res.data)
    },
    async applyAdd(data = { friendName: '' }) {
      return request.post('addReqFriend', data).then(res => res.data)
    },
    async confirmAdd(data = { reqId: '', status: '' }) {
      return request.post('upreqstatus', data).then(res => res.data)
    }
  },
  chat: {
    async history(page = 0, data = { friendName: '' }) {
      return request.post(`getchats/${page}/20`, data).then(res => res.data)
    },
    async sendRedEnvelope(data = { to: '', price: 0 }) {
      return request.post('givepack', data).then(res => res.data)
    },
    async openRedEnvelope(data = { fingerPrint: '', GroupId: undefined }) {
      return request.post('openpack', data).then(res => res.data)
    },
    async sessionList() {
      return request.post('converlist').then(res => res.data)
    },
    async createSession(data = { friendName: '' }) {
      return request.post('createconver', data).then(res => res.data)
    },
    async setRead(data = { msgId: [] }) {
      return request.post('setmsgread', data).then(res => res.data)
    },
    async removeSession(data = { con_id: '' }) {
      return request.post('delcon', data).then(res => res.data)
    }
  },
  group: {
    async create(data = { Name:  '' }) {
      return request.post('crtgrop', data, { plainText: true }).then(res => res.data)
    },
    async import(data = { GroupId: '', Member_Accounts: [] }) {
      return request.post('importgropmember', {
        ...data,
        Member_Accounts: encode(JSON.stringify(data.Member_Accounts))
      }, { plainText: true }).then(res => res.data)
    },
    async list(data = { Member_Account: '' }) {
      return request.post('getjoingrouplist', data, { plainText: true }).then(res => res.data)
    },
    async remove(data = { GroupId: '' }) {
      return request.post('relegrop', data, { plainText: true }).then(res => res.data)
    },
    async removeMember(data = { GroupId: '', Member_Accounts: [] }) {
      return request.post('delgroupmember', data, { plainText: true }).then(res => res.data)
    },
    async memberList(data = { GroupId: '' }) {
      return request.post('idgetmembers', data, { plainText: false }).then(res => res.data)
    },
    async getGroupRecord(data = { GroupId: '' }) {
      return request.post('idgetchats', data, { plainText: true }).then(res => res.data)
    }
  }
}
